package com.hongxun.design.utils;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;

import com.hongxun.design.R;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Admin on 2017/4/19.
 */

public abstract class CommonAdapter<T> extends BaseAdapter {
    protected LayoutInflater mInflater;
    protected Context mContext;
    protected List<T> mDatas;
    protected final int mItemLayoutId;

    protected boolean HasNoData = false;

    public CommonAdapter(Context context, int itemLayoutId) {
        this.mContext = context;
        this.mInflater = LayoutInflater.from(mContext);
        this.mDatas = new ArrayList<>();
        this.mItemLayoutId = itemLayoutId;
    }

    public List<T> getmDatas() {
        return mDatas;
    }

    /**刷新方式更新**/
    public void setData(List<T> list) {
        if (mDatas == null) mDatas = new ArrayList<>();
        clearStatus();
        mDatas.clear();
        mDatas.addAll(list);
        notifyDataSetChanged();
        checkList();
    }
    /**加载方式更新**/
    public void addData(List<T> list) {
        if (mDatas == null) mDatas = new ArrayList<>();
        clearStatus();
        mDatas.addAll(list);
        notifyDataSetChanged();
        checkList();
    }

    /**去掉无数据状态**/
    private void clearStatus() {
        if (HasNoData) {
            HasNoData = false;
            mDatas.clear();
        }
    }

    /**判断是否有数据**/
    private void checkList() {
        if (mDatas == null) mDatas = new ArrayList<>();
        if (mDatas.size() == 0) {
            HasNoData = true;
            notifyDataSetChanged();
        } else {
            HasNoData = false;
        }
    }

    @Override
    public int getCount() {
        if (HasNoData&&mDatas.size()==0)return 1;
        return mDatas.size();
    }

    @Override
    public T getItem(int position) {
        if (HasNoData&&mDatas.size()==0)return null;
        return mDatas.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public int getViewTypeCount() {
        return 2;
    }

    @Override
    public int getItemViewType(int position) {
        if (HasNoData){
            return 1;
        }
        return super.getItemViewType(position);
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
       int viewtype= getItemViewType(position);
        if (getItemViewType(position)==1){
            if (convertView==null){
                convertView=mInflater.inflate(R.layout.list_item_nodata,null);

            }
            return convertView;
        }else{
            final ViewHolder viewHolder = getViewHolder(position, convertView,
                    parent);
            convert(viewHolder, getItem(position));
            return viewHolder.getConvertView();
        }

    }

    public abstract void convert(ViewHolder helper, T item);

    private ViewHolder getViewHolder(int position, View convertView,
                                     ViewGroup parent) {
        return ViewHolder.get(mContext, convertView, parent, mItemLayoutId,
                position);
    }

}